package com.hgy.chapter3.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hgy.framework.annotation.Action;
import com.hgy.framework.annotation.Controller;
import com.hgy.framework.bean.Param;
import com.hgy.framework.bean.View;
import com.hgy.plugin.security.SecurityHelper;
import com.hgy.plugin.security.exception.AuthcException;

/**
 * 处理系统请求
 * 
 * @author alone
 *
 */
@Controller
public class SystemController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SystemController.class);
	
	/**
	 * 首页
	 * 
	 * @return
	 */
	@Action("get:/")
	public View index(){
		return new View("index.jsp");
	}
	
	/**
	 *  进入登录界面
	 *  
	 * @return
	 */
	@Action("get:/login")
	public View login(){
		return new View("login.jsp");
	}
	
	/**
	 * 提交登录表单
	 * 
	 * @param param
	 * @return
	 */
	@Action("post:/login")
	public View loginSubmit(Param param){
		String username = param.getString("username");
		String password = param.getString("password");
		
		try {
			SecurityHelper.login(username, password);
		} catch (AuthcException e) {
			LOGGER.error("login failure", e);
			//失败返回登录界面
			return new View("/login");
		}
		//成功进入客户列表
		return new View("/customer");
	}
	
	/**
	 * 提交注销请求
	 * 
	 * @return
	 */
	@Action("get:/logout")
	public View logout(){
		SecurityHelper.logout();
		return new View("/");
	}
	
	
	
	
	
	
	
	
	
	
	
}
